package Bank;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * 主要测试类 包含统一入口，管理员菜单界面，用户菜单界面，用户银行卡选择界面
 * 
 * @date 2014.8.17
 */
public class MainTest {
//	static Customer[] CustomerArray = new Customer[10];
//	static Admin[] AdminArray = new Admin[10];
//	static Account[] AccountArray = new Account[10];
//	String username;
//	String password;
//	static String username = null;
//	static String password = null;
	static String n = null;
	static Scanner sc = new Scanner(System.in);

	/**
	 * 员工菜单界面
	 */
	public void EmployeeMenu() {
		Employee Employee = new Employee();
		while (true) {
			System.out.println("====================================");
			System.out.println("Welcome To Log In Employee Menu");
			System.out.println("1.Register Manager");
			System.out.println("2.Delete Manager");
			System.out.println("3.Logout");
			System.out.println("====================================");
			System.out.println("Please enter your choice:");
			n = sc.next();
			switch (n) {
			case "1":
				Employee.registerManager();
				break;
			case "2":
				Employee.deleteManager();
				break;
			case "3":
				MainMenu();
				break;
			default:
				System.out.println("Error!Please enter the valid choice!");
				break;
			}
		}
	}

	/**
	 * 管理员菜单界面
	 */
	public void ManagerMenu() {
		Manager manager = new Manager();
		while (true) {
			System.out.println("====================================");
			System.out.println("Welcome To Log In  Manager Menu");
			System.out.println("1. Register Customer");
			System.out.println("2. Register Account");
			System.out.println("3. View Customer details");
			System.out.println("4. View Account details");
			System.out.println("5. Update Customer Details");
			System.out.println("6. Update Account details");
			System.out.println("7. Delete Customer");
			System.out.println("8. Logout");
			System.out.println("====================================");
			System.out.println("Please enter your choice:");
			n = sc.next();
			switch (n) {
			case "1":
				manager.registerCustomer();
				break;
			case "2":
				manager.registerAccount();
				break;
			case "3":
				manager.viewCustomer();
				break;
			case "4":
				manager.viewAccount();
				break;
			case "5":
				manager.customerDetails();
				break;
			case "6":
				manager.accountDetails();
				break;
			case "7":
				manager.deleteCustomer();
				break;
			case "8":
				MainMenu();
				break;
			default:
				System.out.println("Error!Please enter the valid choice!");
				break;
			}
		}
	}

	/**
	 * 用户菜单界面
	 * 
	 * @param customerId
	 * @param accountId
	 */
	public void CustomerMenu(long Customer_id, long Account_id) {
		Customer customer = new Customer();
		Account account = new Account();
		Account.ACCOUNT_ID=Account_id;
		Customer.CUSTOMER_ID=Customer_id;
		while (true) {
			System.out.println("====================================");
			System.out.println("Welcome To Log In Customer Menu");
			System.out.println("1. Deposit");
			System.out.println("2. Withdraw");
			System.out.println("3. Check Balance");
			System.out.println("4. View Transaction History(datewise)");
			System.out.println("5. Change password");
			System.out.println("6. Logout");
			System.out.println("====================================");
			System.out.println("Please enter your choice:");
			n = sc.next();
			switch (n) {
			case "1":
				if (Account_id != -1) {
					account.deposit();
//					customer.deposit(Account_id);
				} else {
					System.out.println("No Account");
				}
				break;
			case "2":
				if (Account_id != -1) {
					account.withdraw();
//					customer.withdraw(Account_id);
				} else {
					System.out.println("No Account");
				}
				break;
			case "3":
				if (Account_id != -1) {
					account.checkBalance();
//					customer.checkBalance(Account_id);
				} else {
					System.out.println("No Account");
				}
				break;
			case "4":
				if (Account_id != -1) {
					account.viewTransactions();
//					customer.transactionHistory(Account_id);
				} else {
					System.out.println("No Account");
				}
				break;
			case "5":
//				 for (int j = 0; j < Customer.getCount(); j++) { // customerId查找数组中对应的位置
//					if (customerId == CustomerArray[j].getCustomer_id()) {
//						CustomerArray[j].changePassword();
//					}
//				} 
				if (Account_id != -1) {
					customer.changePassword();
				} else {
					System.out.println("No Account");
				}
				break;
			case "6":
				MainMenu();
				break;
			default:
				System.out.println("Error!Please enter the valid choice!");
			}
		}
	}

	/**
	 * 用户选择银行账户菜单界面
	 */
	public void ChoiceAccountMenu(long Customer_id) {
		System.out.println("Your have more than one account,"
				+ "Please choice your account:");
		int i = 0;
		int m = 0;
		long array[]=new long[10];
		// for (i = 0; i < Account.getCount(); i++) {
		// if (customerId == AccountArray[i].getCustomer_id()) {
		// System.out.println((i + 1) + ": "
		// + AccountArray[i].getAccount_id());
		// }
		// }
		String Query = "SELECT ACCOUNT_ID FROM ACCOUNT_TABLE WHERE CUSTOMER_ID='"+ Customer_id + "'";
		ResultSet rs = new ConnDB().getrs(Query);
		try {
			while(rs.next()){
				i++;
				System.out.println(i+". Account_id : "+rs.getLong("ACCOUNT_ID"));
				array[i]=rs.getLong("ACCOUNT_ID");
			}
			System.out.println("Choice your Account:");
			n = sc.next();
			m = Integer.valueOf(n);
//			System.out.println(array[m]);
//			System.out.println(Customer_id);
//			this.CustomerMenu(customerId, AccountArray[m - 1].getAccount_id());
			if(array[m]==0){
				System.out.println("Error!Please enter the valid choice!");
				ChoiceAccountMenu(Customer_id);
			}else{
				this.CustomerMenu(Customer_id,array[m]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 页面初始界面，页面统一入口
	 */
	public static void MainMenu() {
		// 定义用户对象
		MainTest obj = new MainTest();
		int count = 0;

		System.out.println("=======Please input your username quit with 'quit'=======");
		String username = sc.next();
		if (username.equals("quit")) {
			System.out.println("=======Thanks for using. GoodBye!=======");
			System.exit(0);
		} else {
			System.out.println("=======Please input your password=======");
			String password = sc.next();
//			boolean customerNotExist = true;
			// long customerId = 0;
			// long accountId = 0;
			String Query = "SELECT EMPLOYEE_PASSWORD,EMPLOYEE_ROLE FROM EMPLOYEE_TABLE WHERE EMPLOYEE_NAME='"
					+ username + "'";
			ResultSet rs = new ConnDB().getrs(Query);
			
			int rt = new ConnDB().usernameIsExist("employee_table", username);
			
			try { 
				if(rt == 1){
					while(rs.next()){
						if (password.equals(rs.getString("EMPLOYEE_PASSWORD"))) {
							if (rs.getNString("EMPLOYEE_ROLE").equals("admin")) {
								obj.EmployeeMenu();
							} else {
								obj.ManagerMenu();
							}
						} else {
							System.out.println("Wrong Password!");
							obj.MainMenu();
						}
					}
				} else {
					rt = new ConnDB().usernameIsExist("customer_table", username);
//					System.out.println(rt);
					if (rt == 1) {
						Query = "SELECT CUSTOMER_PASSWORD,CUSTOMER_ID FROM CUSTOMER_TABLE WHERE CUSTOMER_USERNAME='"
								+ username + "'";
						ResultSet rs1 = new ConnDB().getrs(Query);
						while(rs1.next()){
							if(password.equals(rs1.getString("CUSTOMER_PASSWORD"))){
								long accountId = 0;
//								System.out.println(rs1.getLong("CUSTOMER_ID"));
								Query = "SELECT ACCOUNT_ID FROM ACCOUNT_TABLE WHERE CUSTOMER_ID='"+ rs1.getLong("CUSTOMER_ID") + "'";
								ResultSet rs2 = new ConnDB().getrs(Query);
								while (rs2.next()) {
									count++;
									accountId=rs2.getLong("ACCOUNT_ID");
								}
//								System.out.println(count);
//								System.out.println(accountId);
								if (count > 1) {
									obj.ChoiceAccountMenu(rs1.getLong("CUSTOMER_ID"));
								} else if (count == 1) {
									obj.CustomerMenu(rs1.getLong("CUSTOMER_ID"),accountId);
								} else {
									System.out.println("Your have no account!Please add.");
									obj.CustomerMenu(rs1.getLong("CUSTOMER_ID"),-1);
								}
							} else {
								System.out.println("Wrong Password!");
								obj.MainMenu();
							}
						}
					} else {
						System.out.println("User is not exist!");
						obj.MainMenu();
					}
				}
				
			}	catch (SQLException e) {
				e.printStackTrace();
			
			}
			
		}	
		
			
			
/*			
			try {
				while (rs.next()) {
					if (password == (rs.getInt("EMPLOYEE_PASSWORD"))) {
						customerNotExist = false;
						if (rs.getNString("EMPLOYEE_ROLE").equals("admin")) {
							obj.EmployeeMenu();
						} else {
							obj.ManagerMenu();
						}
					} else {
						Query = "SELECT CUSTOMER_PASSWORD,CUSTOMER_ID FROM CUSTOMER_TABLE WHERE CUSTOMER_USERNAME='"
								+ username + "'";

						ResultSet rs1 = new ConnDB().getrs(Query);
						while (rs1.next()) {
							if (password == (rs1.getLong("CUSTOMER_PASSWORD"))) {
								customerNotExist = false;
								long accountId = 0;
								Query = "SELECT ACCOUNT_ID FROM ACCOUNT_TABLE WHERE CUSTOMER_ID='"+ rs.getLong("CUSTOMER_ID") + "'";
								ResultSet rs2 = new ConnDB().getrs(Query);
								if (rs2.next()) {
									count++;
									accountId=rs2.getLong("ACCOUNT_ID");
								}
								if (count > 1) {
									obj.ChoiceAccountMenu(rs1.getLong("CUSTOMER_ID"));
								} else if (count == 1) {
									obj.CustomerMenu(rs1.getLong("CUSTOMER_ID"),accountId);
								} else {
									System.out.println("Your have no account!Please add.");
									obj.CustomerMenu(rs1.getLong("CUSTOMER_ID"),-1);
								}
							} else {
								System.out.println("Wrong Password!");
								obj.MainMenu();
							}
						}
					}
				}
				if (customerNotExist == true) {
					System.out.println("User is not exist!");
					obj.MainMenu();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
*/
		/*
		 * if (username.equals("admin") && password.equals("admin")) {
		 * obj.AdminMenu(); obj.Employee(); } else { int count = 0; /* if
		 * (Customer.getCount() > 0) { // 判断customer数组是否为空 for (int i = 0; i <
		 * Customer.getCount(); i++) { if (username.equals(CustomerArray[i]
		 * .getCustomer_username())) { // 判断用户是否存在 customerNotExist = false; if
		 * (password.equals(CustomerArray[i] .getCustomer_password())) { // 判断密码
		 * customerId = CustomerArray[i].getCustomer_id(); if
		 * (Account.getCount() > 0) { for (int j = 0; j < Account.getCount();
		 * j++) { if (customerId == AccountArray[j] .getCustomer_id()) {
		 * accountId = AccountArray[j] .getAccount_id(); count++; } } break; } }
		 * else { System.out.println("wrong password"); obj.MainMenu(); } } } if
		 * (customerNotExist == true) {
		 * System.out.println("Customer name is not exist");
		 * MainTest.MainMenu(); } else { if (count == 0) { // 用户账户正确但是没有银行卡账户
		 * System.out .println("You hava no account!please add");
		 * obj.CustomerMenu(customerId, -1);// -1做一个标记，该用户没有银行卡账户 } else if
		 * (count > 1) { // 多个银行卡账户，调用用户选择账户菜单
		 * obj.ChoiceAccountMenu(customerId); } else { // 只用一个银行卡账户，直接进入，没有选择菜单
		 * obj.CustomerMenu(customerId, accountId); } } } else {
		 * System.out.println("There is no customer"); MainTest.MainMenu(); }
		 * 
		 * }
		 */

	}

	public static void main(String args[]) {
		MainTest object = new MainTest(); // 调用初始界面
		object.MainMenu();
	}
}
